<template>
	<view class="padding-lr margin-tb bg-white">
		<u-form :model="info" ref="uForm">
			<u-form-item label="组织代码" label-width="auto" prop="orgCode" required><u-input v-model="info.orgCode" /></u-form-item>
			<u-form-item label="组织名称" label-width="auto" prop="orgName" required><u-input v-model="info.orgName" /></u-form-item>
			<u-form-item label="组织简称" label-width="auto" prop="aliasName" required><u-input v-model="info.aliasName" /></u-form-item>
			<u-form-item v-if="info.orgTypeCode!='COMPANY'" label="组织类型" label-width="auto" prop="orgTypeCode" required>
				<cmp-select v-model="info.orgTypeCode" :list="orgTypeOptions" value-name="orgTypeCode" label-name="orgTypeName"></cmp-select>
			</u-form-item>
			<u-form-item v-if="info.orgTypeCode!='COMPANY'" label="序号" label-width="auto" prop="sequence" required><u-input type="number" v-model="info.sequence" /></u-form-item>
			<view class="padding-tb"><u-button type="primary" @click="save">保存</u-button></view>
		</u-form>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		if (option.info) {
			this.info = JSON.parse(option.info);
		} else {
			this.info.parentId = option.parentId || '';
		}
	},
	data() {
		return {
			info: {
				id: '',
				orgCode: '',
				orgName: '',
				aliasName: '',
				orgTypeCode: '',
				orgTypeName: '',
				sequence: '1',
				parentId: ''
			}, // 组织信息
			orgRules: {
				orgCode: [{ required: true, message: '必填', trigger: ['blur', 'change'] }],
				orgName: [{ required: true, message: '必填', trigger: ['blur', 'change'] }],
				aliasName: [{ required: true, message: '必填', trigger: ['blur', 'change'] }],
				orgTypeCode: [{ required: true, message: '必填', trigger: ['blur', 'change'] }]
			},
			orgTypeOptions: [
				{
					orgTypeCode: 'BRANCH',
					orgTypeName: '分公司'
				},
				{
					orgTypeCode: 'DEPARTMENT',
					orgTypeName: '部门'
				},
				{
					orgTypeCode: 'GROUP',
					orgTypeName: '小组'
				},
				{
					orgTypeCode: 'AUTH_GROUP',
					orgTypeName: '特别小组'
				}
			]
		};
	},
	onReady: function() {
		this.$refs['uForm'].setRules(this.orgRules);
	},
	watch: {
		'info.orgCode': function() {
			this.info.orgCode = (this.info.orgCode || '').toUpperCase();
		}
	},
	methods: {
		// 保存组织信息
		save: function() {
			if (!this.info.parentId) {
				this.$utils.showToast('无效的父组织');
				return;
			}
			this.$refs['uForm'].validate(valid => {
				if (valid) {
					let url = '/admin/sys/org/save';
					if (this.info.orgTypeCode == 'COMPANY') {
						url = '/admin/sys/org/changeTopOrg'
					}
					this.$request.post(url, this.info, res => {
						this.$utils.showModal("保存成功！", () => {
							uni.navigateBack({
								delta: 1
							});
						});
					}, true, true, true)
				}
			});
		},
	}
};
</script>

<style lang="scss"></style>
